package com.sz.admin.pendingpost.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.pendingpost.service.PendingPostService;
import com.sz.admin.pendingpost.pojo.po.PendingPost;
import com.sz.admin.pendingpost.mapper.PendingPostMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.pendingpost.pojo.dto.PendingPostCreateDTO;
import com.sz.admin.pendingpost.pojo.dto.PendingPostUpdateDTO;
import com.sz.admin.pendingpost.pojo.dto.PendingPostListDTO;
import com.sz.admin.pendingpost.pojo.dto.PendingPostImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.pendingpost.pojo.vo.PendingPostVO;

/**
 * <p>
 * 待审核帖子记录 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-06-30
 */
@Service
@RequiredArgsConstructor
public class PendingPostServiceImpl extends ServiceImpl<PendingPostMapper, PendingPost> implements PendingPostService {
    @Override
    public void create(PendingPostCreateDTO dto){
        PendingPost pendingPost = BeanCopyUtils.copy(dto, PendingPost.class);
        save(pendingPost);
    }

    @Override
    public void update(PendingPostUpdateDTO dto){
        PendingPost pendingPost = BeanCopyUtils.copy(dto, PendingPost.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(PendingPost::getPendingId, dto.getPendingId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(pendingPost);
    }

    @Override
    public PageResult<PendingPostVO> page(PendingPostListDTO dto){
        Page<PendingPostVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), PendingPostVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<PendingPostVO> list(PendingPostListDTO dto){
        return listAs(buildQueryWrapper(dto), PendingPostVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public PendingPostVO detail(Object id){
        PendingPost pendingPost = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(pendingPost);
        return BeanCopyUtils.copy(pendingPost, PendingPostVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<PendingPostImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), PendingPostImportDTO.class, true);
        List<PendingPostImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(PendingPostListDTO dto, HttpServletResponse response) {
        List<PendingPostVO> list = list(dto);
        String fileName = "待审核帖子记录模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "待审核帖子记录", PendingPostVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(PendingPostListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(PendingPost.class);
        if (Utils.isNotNull(dto.getPostId())) {
            wrapper.eq(PendingPost::getPostId, dto.getPostId());
        }
        if (Utils.isNotNull(dto.getSubmitTimeStart()) && Utils.isNotNull(dto.getSubmitTimeEnd())) {
            wrapper.between(PendingPost::getSubmitTime, dto.getSubmitTimeStart(), dto.getSubmitTimeEnd());
        }
        if (Utils.isNotNull(dto.getIsDeleted())) {
            wrapper.eq(PendingPost::getIsDeleted, dto.getIsDeleted());
        }
        return wrapper;
    }
}